package com.itboye.ihomebank.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.net.NetPublicParams;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.DebugLog;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 登录相关控制类--状态模式</br></br>
 * 
 * 登录成功后调用setLoginState(new LoginedState());</br></br> 退出登录后调用setLoginState(new
 * UnLoginState());</br></br> 进入主Activity前一定要设置一次，要么登录要么未登录</br></br>
 * 
 * @author Young
 * 
 */
public class LoginController implements Observer {

	 static UserPresenter userPresenter;
	private static ILoginState loginState = new UnLoginState();

	public static void setLoginState(ILoginState loginState) {

		LoginController.loginState = loginState;
		if (loginState instanceof UnLoginState) {
			DebugLog.v("bytag", "未登录。。。");
			// PushAgent mPushAgent = PushAgent.getInstance(App.ctx);
			// mPushAgent.disable(new IUmengUnregisterCallback() {
			//
			// @Override
			// public void onUnregistered(String arg0) {
			// // TODO Auto-generated method stub
			// DebugLog.v("bytag", "停止推送:"+arg0);
			// }
			// });
		} else if (loginState instanceof LoginedState) {
			DebugLog.v("bytag", "已登录。。。");
			String scienerUsername = SPUtils.get(MyApplcation.ctx, null,
					SPContants.ScienerUsername, "") + "";// 绑定科技侠账号，科技侠账号,为空未绑定
			if(scienerUsername.equals("")){//未绑定科技侠账号
				ByAlert.alert("未绑定可加下");
				bindKeJiXia();
			}else{
				ByAlert.alert("已经绑定");
			}
			// PushAgent mPushAgent = PushAgent.getInstance(App.ctx);
			// mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
			// mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
			// mPushAgent.onAppStart();
			// 开启推送并设置注册的回调处理
			// if (!mPushAgent.isEnabled()) {
			// mPushAgent.enable(new IUmengRegisterCallback() {

			// @Override
			// public void onRegistered(String registrationId) {
			// onRegistered方法的参数registrationId即是device_token

			// setExclusiveAlias(registrationId);
			// }
			// });
			// }

			// String device_token = UmengRegistrar.getRegistrationId(App.ctx);
			// if (!TextUtils.isEmpty(device_token)) {
			// setExclusiveAlias(device_token);
			// }

		}
	}

	private static void bindKeJiXia() {
		// TODO Auto-generated method stub
//		
//		RequestQueue requestQueue = Volley.newRequestQueue(MyApplcation.ctx);
//		Map<String, String> map = new HashMap<String, String>();  
//		map.put("name1", "value1");  
//		map.put("name2", "value2");  
//		JSONObject jsonObject = new JSONObject(map);
//		JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST,NetPublicConstant.URL, jsonObject,
//		    new Response.Listener<JSONObject>() {
//		        @Override
//		        public void onResponse(JSONObject response) {
//		            Log.d(TAG, "response -> " + response.toString());
//		        }
//		    }, new Response.ErrorListener() {
//		        @Override
//		        public void onErrorResponse(VolleyError error) {
//		            Log.e(TAG, error.getMessage(), error);
//		    }
//		    })
//		    {
//		    //注意此处override的getParams()方法,在此处设置post需要提交的参数根本不起作用
//		    //必须象上面那样,构成JSONObject当做实参传入JsonObjectRequest对象里
//		    //所以这个方法在此处是不需要的
////		    @Override
////		    protected Map<String, String> getParams() {                
////		          Map<String, String> map = new HashMap<String, String>();  
////		            map.put("name1", "value1");  
////		            map.put("name2", "value2");  
//		                
////		        return params;
////		    }
//		            
//		    @Override
//		    public Map<String, String> getHeaders() {
//		    	  HashMap<String, String> headers = new HashMap<String, String>();
//		    	// 公共参数
//		    	headers.put(NetPublicParams.APP_VERSION, "1.0.0");
//		    	headers.put(NetPublicParams.APP_TYPE, "android");
////		    	headers.put(NetPublicParams.TIME, time);
//		    	headers.put(NetPublicParams.ALG, NetPublicParams.ALG_VALUE);
////		    	headers.put(NetPublicParams.NOTIFY_ID, time);
//		    	headers.put("client_id", NetPublicParams.CLIENT_ID);
////				this.url = url + "?client_id=" + NetPublicParams.CLIENT_ID;
//		    	
//		    	
//		      
////		        headers.put("Accept", "application/json");
////		        headers.put("Content-Type", "application/json; charset=UTF-8");
//		                
//		        return headers;
//		    }
//		};
//		requestQueue.add(jsonRequest);
	}

	private static void setExclusiveAlias(String registrationId) {
		String uid = (String) SPUtils.get(MyApplcation.ctx, null,
				SPContants.USER_ID, "");
		// PushAgent mPushAgent = PushAgent.getInstance(MyApplcation.ctx);
		// mPushAgent.setExclusiveAlias(uid, "sunsun");
		DebugLog.v("bytag", "setExclusiveAlias   token:", registrationId,
				" alian:", uid, "  type:", "sunsun");

		// //登录后绑定设备
		// MyJsonRequest<String> request = new
		// MyJsonRequest.Builder<String>().apiVer("100")
		// .typeKey("BY_Device_bind")
		// .param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
		// ""))
		// .param("device_token",registrationId)
		// .param("version",android.os.Build.VERSION.RELEASE)
		// .param("device_type","Android")
		// .requestListener(new XRequestListener<String>() {
		//
		// @Override
		// public void onResponse(String response) {
		//
		// }
		// }).errorListener(new XErrorListener() {
		//
		// @Override
		// public void onErrorResponse(Exception exception, int code, String
		// msg) {
		// if (exception instanceof CodeErrorException) {
		// Toast.makeText(App.ctx, msg, 0).show();
		// } else {
		// Toast.makeText(App.ctx, exception.toString(), 0).show();
		// }
		//
		// }
		// }).build();
		// HttpRequest.getDefaultRequestQueue().add(request);
		//

	}

	public static void onMyMoneyClick(Activity act, Object data) {
		loginState.onMyMoneyClick(act, data);
	}

	public static void onMyAppointmentClick(Activity act, Object data) {
		loginState.onMyAppointmentClick(act, data);
	}

	public static void onMyCollectionClick(Activity act, Object data) {
		loginState.onMyCollectionClick(act, data);
	}

	public static void onMySignClick(Activity act, Object data) {
		loginState.onMySignClick(act, data);
	}

	public static void onMyPersonalCenter(Activity act, Object data) {
		loginState.onMyPersonalCenter(act, data);
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		
	}
}
